package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.service.CartService;
import com.changgou.pojo.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 获取购物车
     * @param username 用户名
     * @return
     */
    @GetMapping("/{username}")
    public Result findCart(@PathVariable String username){
        Map cart =  cartService.findCart( username );
        return new Result( true, StatusCode.OK,"查询成功",cart );
    }


    /**
     * 添加商品到购物车
     * @param username 用户名
     * @param map  ( 包括两个key skuId,num )
     * @return
     */
    @PostMapping("/{username}")
    public Result add(@PathVariable String username ,@RequestBody Map map  ){
        cartService.add( username  , (String)map.get( "skuId" ),  (Integer) map.get( "num" )  );
        return new Result(  );
    }


    /**
     * 更改购物车勾选状态
     * @param username  用户名
     * @param map （包括两个key skuId,checked ）
     * @return
     */
    @PutMapping("/checked/{username}")
    public Result updateChecked( @PathVariable String username ,@RequestBody Map map  ){
        boolean b = cartService.updateChecked( username, (String) map.get( "skuId" ), (boolean) map.get( "checked" ) );
        if(b){
            return new Result(  );
        }else{
            return new Result( false,StatusCode.OK,"没有找到商品" );
        }
    }

    /**
     * 删除选中的购物车
     * @param username
     * @return
     */
    @DeleteMapping("/checked/{username}")
    public Result deleteChecked( @PathVariable String username  ){
        cartService.deleteChecked( username );
        return new Result(  );
    }

    /**
     * 移到收藏夹
     * @param username
     * @return
     */
    @PutMapping("/collect/{username}")
    public Result collectChecked(@PathVariable String username){
        cartService.collectChecked( username );
        return new Result(  );
    }


    /**
     * 收藏夹列表
     * @param username
     * @return
     */
    @GetMapping("/collect/{username}")
    public Result getCollect(@PathVariable String username){
        List<OrderItem> collect = cartService.getCollect(username);
        return new Result(true,StatusCode.OK,"查询成功",collect);
    }
}
